<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class Index extends Controller
{
    // 首页
    public function index()
    {
        $search = input('search');
        // 接收 Session 中的值
        $useres = Session::get('user');
        // 问题用户表联查
        if (empty($search)){
            $list = Db::table(['zz_problem'=>'p','zz_user'=>'u','zz_cate'=>'c'])
                    ->where('p.zz_uid = u.zz_id')
                    ->where('c.zz_cate_id = p.zz_problem_cid')
                    ->paginate(10);
        }else{
            $list = Db::table(['zz_problem'=>'p','zz_user'=>'u','zz_cate'=>'c'])
                    ->where('p.zz_uid = u.zz_id')
                    ->where('zz_problem_tit','like','%'.$search.'%')
                    ->where('c.zz_cate_id = p.zz_problem_cid')
                    ->paginate(10,false,['query'=>request()->param()]);
        }

        // 查询所有问题分类
        $cate = Db::table('zz_cate')->where('zz_cate_status',1)->select();
        // 查询所有评论
        $comment = Db::table('zz_comment')->select();
        // 将评论以问题做分类
        $num = [];
        for ($i=0;$i<sizeof($list);$i++){
            for ($j=0;$j<sizeof($comment);$j++){
                if (!empty($list[$i]['zz_problem_id'])){
                    if ($list[$i]['zz_problem_id'] == $comment[$j]['zz_problem_id']){
                        $num[$list[$i]['zz_problem_id']][] = $list[$i]['zz_problem_id'];
                    } elseif(empty($num[$list[$i]['zz_problem_id']])){
                        $num[$list[$i]['zz_problem_id']] = null;
                    }
                }
            }
        }

        return view('index/index',[
            'useres' => $useres,
            'list' => $list,
            'cate' => $cate,
            'num' => $num
        ]);
    }

    public function doCate($name)
    {
        // 获取Session 中的数据
        $useres = Session::get('user');
        
        // 查询所有问题分类
        $cate = Db::table('zz_cate')->where('zz_cate_status',1)->select();
        // 问题用户表联查
        $list = Db::table(['zz_problem'=>'p','zz_user'=>'u','zz_cate'=>'c'])
            ->where('p.zz_uid = u.zz_id')
            ->where('c.zz_cate_id = p.zz_problem_cid')
            ->where('c.zz_cate_name',$name)
            ->paginate(10);
        // 查询所有评论
        $comment = Db::table('zz_comment')->select();
        // 将评论以问题做分类
        $num = [];
        for ($i=0;$i<sizeof($list);$i++){
            for ($j=0;$j<sizeof($comment);$j++){
                if ($list[$i]['zz_problem_id'] == $comment[$j]['zz_problem_id']){
                    $num[$list[$i]['zz_problem_id']][] = $list[$i]['zz_problem_id'];
                } elseif(empty($num[$list[$i]['zz_problem_id']])){
                    $num[$list[$i]['zz_problem_id']] = null;
                }
            }
        }

        return view('index/pbcate',[
            'useres' => $useres,
            'list' => $list,
            'cate' => $cate,
            'num' => $num,
            'name' => $name
        ]);
    }

    // 访问 提问用户个人主页
    public function toCenter($id)
    {
        // 获取Session 中的数据
        $useres = Session::get('user');
        // 判断有无Session
        if (empty($useres)) {
            $msg = "您还没有登录！";
            $url = "../log/login";
            return $this->error($msg,$url);
        }
        // 通过 用户id 查询用户信息
        $user = Db::table('zz_user')->where('zz_id',$id)->find();
        // 生成当前时间
        $time['date'] = date('Y年m月');
        $time['day'] = date('d');
        // 查询 用户提问
        $userProblem = Db::table('zz_problem')->where('zz_uid',$id)->select();
        // 统计总提问个数
        $userProNum = sizeof($userProblem);
        $user['userProNum'] = $userProNum;
        // 查询用户评价
       $userComment = Db::table(['zz_comment'=>'c','zz_problem'=>'p'])
            ->where('c.zz_comment_uid =' .$id)
            ->where('c.zz_problem_id = p.zz_problem_id')
            ->select();
        // 查询用户收藏
        $userCollect = Db::table('zz_collect')->where('zz_user_id',$id)->select();
        // 通过收藏问题id 查询问题
        $userProblemCollect = [];
        if (!empty($userCollect)){
            for ($i=0;$i<sizeof($userCollect);$i++){
                $userProblemCollect[] = Db::table('zz_problem')->where('zz_problem_id',$userCollect[$i]['zz_problem_id'])->find();
            }
        }
        // 查询 关注
        $followUser = Db::table('zz_follow')->where('zz_user_id',$id)->select();
        $followUserInf = [];
        if (!empty($followUser)){
            for ($i=0;$i<sizeof($followUser);$i++){
                $followUserInf[] = Db::table('zz_user')->where('zz_id',$followUser[$i]['zz_concern_id'])->find();
            }
        }
        // 查询 粉丝 个数 并统计个数
        $userFollow = Db::table('zz_follow')->where('zz_concern_id',$id)->select();
        $userFollowInf = [];
        if (!empty($userFollow)){
            for ($i=0;$i<sizeof($userFollow);$i++){
                $userFollowInf[] = Db::table('zz_user')->where('zz_id',$userFollow[$i]['zz_user_id'])->find();
            }
        }
        // 判断 查看用户是否是当前登录用户
        $userSession = Session::get('user');
        $userIdNow = Db::table('zz_user')->field('zz_id')->where('zz_name',$userSession['name'])->find();
        if ($userIdNow['zz_id'] == $id ){
            $user['who'] = 1;
        }
        $user['userIdNow'] = $userIdNow['zz_id'];

        return view('index/tocenter',[
            'user' => $user,
            'time' => $time,
            'userProblem' => $userProblem,
            'userComment' => $userComment,
            'userProblemCollect' => $userProblemCollect,
            'followUserInf' => $followUserInf,
            'userFollowInf' => $userFollowInf
        ]);
    }

    // 关注
    public function doFollow()
    {
        // 接收被关注者id
        $uid = input('uid');
        // 接收关注者id
        $nid = input('nid');
        // 判断是否已经关注
        $res = Db::table('zz_follow')->where('zz_user_id',$nid)->where('zz_concern_id',$uid)->find();
        if ($res){
            $res['status'] = 0;
            return json($res);
        }

        $data['zz_user_id'] = $nid;
        $data['zz_concern_id'] = $uid;
        Db::table('zz_follow')->insert($data);

        $res['status'] = 1;
        return json($res);
    }

    // 取消关注
    public function doDisFollow()
    {
        // 接收被关注者id
        $uid = input('uid');
        // 接收关注者id
        $nid = input('nid');
        // 判断是否已经取消关注
        $res = Db::table('zz_follow')->where('zz_user_id',$nid)->where('zz_concern_id',$uid)->find();
        if (!$res){
            $res['status'] = 0;
            return json($res);
        }

        Db::table('zz_follow')->where('zz_user_id',$nid)->where('zz_concern_id',$uid)->delete();

        $res['status'] = 1;
        return json($res);
    }



}
